package mi.payment.controller;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import mi.payment.R;
import mi.payment.ui.print.helper.SumTotal;
import mi.payment.ui.print.helper.TotalSettlementDetail;
import mi.payment.constants.Params;
import mi.payment.db.PayDetailModel;
import mi.payment.db.dao.PayDetail;
import mi.payment.settings.MerManagerSettings;
import mi.payment.settings.MerchantSettingsOperation;
import mi.payment.settings.SystemParamsOperation;
import mi.payment.settings.SystemParamsSettings;
import mi.payment.ui.base.ParentActivity;
import mi.payment.ui.other.SettlementResultActivity;
import mi.payment.utils.transaction.BatchSettlementTask;
import sunmi.payment.ui.dialog.LoadDialog;

public class SettlementCtrl extends BaseController {

    public SettlementCtrl(ParentActivity activity) {
        super(activity);
    }

    @SuppressLint("StaticFieldLeak")
    public void settlement() {
        BatchSettlementTask task = new BatchSettlementTask(mActivity) {

            @Override
            protected void onPostExecuteImpl(String code) {
                mActivity.openActivity(SettlementResultActivity.class);
                if (Params.sL3Params != null) {
                    mActivity.finish();
                }
            }

        };
        LoadDialog dialog = mActivity.getLoadingDialog(R.string.load);
        task.setLoadDialog(dialog);
        task.execute();
    }

    public static List<PayDetail> calTransDetailsList() {
        PayDetailModel payDetailModel = PayDetailModel.getInstance();
        List<PayDetail> list = payDetailModel.getTransactionList();
        return list;
    }

    /**
     * 银行卡结算总计单
     */
    public static TotalSettlementDetail calSettlementDetail() {
        PayDetailModel payDetailModel = PayDetailModel.getInstance();
        SystemParamsSettings systemParamsSettings = SystemParamsOperation.getInstance().getSystemParamsSettings();
        MerManagerSettings merManagerSettings = MerchantSettingsOperation.getInstance().getMerManagerSettings();
        String batchNumber = systemParamsSettings.getBatchNumStart();
        TotalSettlementDetail totalSettlementDetail = new TotalSettlementDetail();
        totalSettlementDetail.shopName = merManagerSettings.getMerchantName();
        totalSettlementDetail.shopNumber = merManagerSettings.getMerchantNum();
        totalSettlementDetail.terminalNumber = merManagerSettings.getTerminalNum();
        // 批次号
        totalSettlementDetail.batchNumber = batchNumber;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        // 日期
        totalSettlementDetail.date = formatter.format(new Date());
        SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
        totalSettlementDetail.time = timeFormatter.format(new Date());
        totalSettlementDetail.sumTotalsNotForeign = payDetailModel.getSumTotalNotForeignCard();
        totalSettlementDetail.sumTotalsForeign = payDetailModel.getSumTotalForeignCard();
        totalSettlementDetail.sumTotalsQRCode = payDetailModel.getSumTotalQRCode();

        int totalNum = 0;
        long income = 0; // 收款总金额
        long totalRefund = 0;// 退款总金额

        List<SumTotal> sumTotalsNotForeign = totalSettlementDetail.sumTotalsNotForeign;
        if (sumTotalsNotForeign != null && !sumTotalsNotForeign.isEmpty()) {
            for (SumTotal s : sumTotalsNotForeign) {
                totalNum += Integer.parseInt(s.getTransTimes());
                if (TextUtils.equals("25", s.getTransType()) || TextUtils.equals("21", s.getTransType()) || TextUtils.equals("23", s.getTransType())) {
                    // 退款
                    totalRefund += Long.parseLong(s.getSumMoney());
                } else {
                    // 收入
                    income += Long.parseLong(s.getSumMoney());
                }
            }
        }

        List<SumTotal> sumTotalsQRCode = totalSettlementDetail.sumTotalsQRCode;
        if (sumTotalsQRCode != null && !sumTotalsQRCode.isEmpty()) {
            for (SumTotal s : sumTotalsQRCode) {
                if (TextUtils.equals("0", s.getTransTimes())) {
                    continue;
                }
                totalNum += Integer.parseInt(s.getTransTimes());
                if (s.getTransType().contains("R") || s.getTransType().contains("D")) {
                    // 退款
                    totalRefund += Long.parseLong(s.getSumMoney());
                } else {
                    // 收入
                    income += Long.parseLong(s.getSumMoney());
                }
            }
        }

        totalSettlementDetail.totalNum = String.valueOf(totalNum);
        totalSettlementDetail.totalMoney = (income - totalRefund) + "";

        // 外卡
        totalNum = 0;
        income = 0;
        totalRefund = 0;
        List<SumTotal> sumTotalsForeign = totalSettlementDetail.sumTotalsForeign;
        if (sumTotalsForeign != null && !sumTotalsForeign.isEmpty()) {
            for (SumTotal s : sumTotalsForeign) {
                totalNum += Integer.parseInt(s.getTransTimes());
                if (TextUtils.equals("25", s.getTransType()) || TextUtils.equals("21", s.getTransType())) {
                    // 退款
                    totalRefund += Long.parseLong(s.getSumMoney());
                } else {
                    // 收入
                    income += Long.parseLong(s.getSumMoney());
                }
            }
        }
        totalSettlementDetail.totalNumForeign = String.valueOf(totalNum);
        totalSettlementDetail.totalMoneyForeign = (income - totalRefund) + "";
        DataCollectionCtrl.settlementSum(payDetailModel, totalSettlementDetail.shopNumber); // 结算数据上送到dataService
        return totalSettlementDetail;
    }


}
